package com.xx.pmis.web.actions;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Write;
import com.xx.pmis.services.WriteService;
import com.xx.pmis.web.forms.WriteForm;
import com.xx.pmis.web.util.CurrentDate;

/**
 * @class 涂鸦作品管理Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class WriteAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(WriteAction.class);

	WriteService writeService = getPMISServices().getWriteService();

	// 查询所有涂鸦作品
	public ActionForward listWrite(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> all = null;
		try {
			all = writeService.getAllWrite();
		} catch (Exception e) {
			String errorPage = "【文档管理/涂鸦作品】查询【涂鸦作品】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("writeList", all);
		request.setAttribute("position", "文档管理/涂鸦作品");
		return mapping.findForward("listWrite");
	}

	// 新增涂鸦作品
	public ActionForward addWrite(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("position", "涂鸦作品/新增");
		return mapping.findForward("addWrite");
	}

	// 新增，处理方法
	public ActionForward addWrite_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		WriteForm f = (WriteForm) form;
		String writeTitle = f.getWriteTitle(); // 标题
		String WriteText = f.getWriteText(); // 正文
		String WriteType = f.getWriteType();// 类型，0 诗歌散文 1 影评书评 2 长篇小说
		String attachInfo = f.getAttachInfo();// 关系人/作品

		Write write = new Write();
		write.setEditTime(CurrentDate.currentDateAndTime());
		write.setWriteDate(CurrentDate.currentDate());
		write.setWriteTitle(writeTitle);
		write.setWriteText(WriteText);
		write.setWriteType(WriteType);
		write.setAttachInfo(attachInfo);
		write.setEditNum("1");

		try {
			writeService.saveOrUpdateWrite(write);
		} catch (Exception e) {
			String errorPage = "【文档管理/涂鸦作品/新增】保存或更新【涂鸦作品】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listWriteByPage(mapping, form, request, response);
		return mapping.findForward("listWrite");
	}

	// 编辑
	public ActionForward editWrite(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String WriteID = request.getParameter("writeID");

		Write write = null;
		try {
			write = writeService.getWriteByWriteID(WriteID);
		} catch (Exception e) {
			String errorPage = "【文档管理/涂鸦作品/编辑】根据【涂鸦作品ID】查询该【涂鸦作品】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("write", write);
		request.setAttribute("position", "涂鸦作品/编辑");
		return mapping.findForward("editWrite");
	}

	// 编辑，处理方法
	public ActionForward editWrite_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		WriteForm f = (WriteForm) form;
		String writeID = f.getWriteID();
		String writeTitle = f.getWriteTitle(); // 标题
		String WriteText = f.getWriteText(); // 正文
		String WriteType = f.getWriteType();// 类型，0 诗歌散文 1 影评书评 2 长篇小说
		String attachInfo = f.getAttachInfo();// 关系人/作品

		Write write = null;
		try {
			write = writeService.getWriteByWriteID(writeID);
		} catch (Exception e) {
			String errorPage = "【文档管理/涂鸦作品/编辑】根据【涂鸦作品ID】查询该【涂鸦作品】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		write.setEditTime(CurrentDate.currentDateAndTime());
		write.setWriteTitle(writeTitle);
		write.setWriteText(WriteText);
		write.setWriteType(WriteType);
		write.setAttachInfo(attachInfo);
		write.setEditNum(Integer.toString(Integer.parseInt(write.getEditNum()) + 1));

		try {
			writeService.saveOrUpdateWrite(write);
		} catch (Exception e) {
			String errorPage = "【文档管理/涂鸦作品/编辑】保存或更新【涂鸦作品】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		listWriteByPage(mapping, form, request, response);
		return mapping.findForward("listWrite");
	}

	// 删除
	public ActionForward delWrite(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Write write = null;
		WriteForm f = (WriteForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				write = writeService.getWriteByWriteID(ids[i]);
			} catch (Exception e) {
				String errorPage = "【文档管理/涂鸦作品/删除】根据【涂鸦作品ID】查询该【涂鸦作品】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			try {
				writeService.deleteWrite(write);
			} catch (Exception e) {
				String errorPage = "【文档管理/涂鸦作品/删除】保存或更新【涂鸦作品】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
		}

		listWriteByPage(mapping, form, request, response);
		return mapping.findForward("listWrite");
	}

	// 明细
	public ActionForward detailWrite(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String writeID = request.getParameter("writeID");

		Write write = null;
		try {
			write = writeService.getWriteByWriteID(writeID);
		} catch (Exception e) {
			String errorPage = "【文档管理/涂鸦作品/明细】根据【涂鸦作品ID】查询该【涂鸦作品】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("write", write);
		request.setAttribute("position", "涂鸦作品/明细");
		return mapping.findForward("detailWrite");
	}

	// 分页显示
	public ActionForward listWriteByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = writeService.getCountOfWrite();
		} catch (Exception e) {
			String errorPage = "【文档管理/涂鸦作品】查询【涂鸦作品】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}

		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<?> all = null;
		try {
			all = writeService.getAllWriteByPage(Integer.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			String errorPage = "【文档管理/涂鸦作品】查询【涂鸦作品】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("writeList", all);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}

		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}
		
		if(totalPage == 0) {
			request.setAttribute("pageNum", "0");
			request.setAttribute("noUp", "yes");
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "write.do");
		request.setAttribute("position", "文档管理/涂鸦作品");
		return mapping.findForward("listWrite");
	}
}